<template>
  <div>
    <!-- 卡片视图区 -->
    <el-card class="box-card">
      <!-- 面包屑导航区 -->
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>留言管理</el-breadcrumb-item>
        <el-breadcrumb-item>留言管理</el-breadcrumb-item>
      </el-breadcrumb>

      <!-- 搜索区 -->
      <el-row :gutter="20">
        <el-col :span="6">
          <el-input
            placeholder="请输入需要搜索的留言人姓名"
            v-model="queryInfo.query"
            clearable>
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="searchMessage">
            </el-button>
          </el-input>
        </el-col>
        <el-col :span="2">
          <el-dropdown>
            <span class="el-dropdown-link">
              显示留言<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="displayAll">显示全部留言</el-dropdown-item>
              <el-dropdown-item @click.native="displayAudited">显示已审核留言</el-dropdown-item>
              <el-dropdown-item @click.native="displayUnaudited">显示未审核留言</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
        <el-col :span="2">
          <el-button type="primary" @click="addClick">添加留言</el-button>
        </el-col>
        <el-col :span="2">
          <el-button type="danger" @click="batchDelete">批量删除</el-button>
        </el-col>
        <el-col :span="4">
          <el-button type="warning" @click="batchRevers">反选</el-button>
        </el-col>
      </el-row>

      <!-- 留言表格区 -->
      <el-table
        :header-cell-style="{textAlign: 'center'}"
        :cell-style="{ textAlign: 'center' }"
        :data="tableData"
        @selection-change="handleSelectionChange"
        border
        style="width: 100%">
        <el-table-column
          type="selection"
          width="55">
        </el-table-column>
        <el-table-column
          prop="id"
          label="序号"
          width="180">
        </el-table-column>
        <el-table-column
          prop="name"
          label="留言人姓名"
          width="180">
        </el-table-column>
        <el-table-column
          label="留言时间">
          <template slot-scope="scope">
            {{scope.row.createtime | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column
          prop="content"
          label="留言内容">
        </el-table-column>
        <el-table-column
          prop="display"
          label="审核">
          <template slot-scope='scope'>
            <el-button 
              v-if="scope.row.display=='1'" 
              type="success" 
              size="mini"
              icon="el-icon-check"
              @click="batchRever(scope.row.id)"></el-button>
            <el-button
              v-else
              type="danger"
              size="mini" 
              icon="el-icon-close"
              @click="batchRever(scope.row.id)"></el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button 
              type="primary" 
              icon="el-icon-edit" 
              size="mini"
              @click="editClick(scope.row)"></el-button>
            <el-button 
              type="danger" 
              icon="el-icon-delete" 
              size="mini" 
              @click="delMessage(scope.row.id)"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="displayInfo.pageNum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="displayInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background>
      </el-pagination>
    </el-card>

    <!-- 操作留言对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="opeDialogVisible"
      width="50%">
      <el-form
        :model="opeForm"
        ref="opeFormRef"
        label-width="100px">
        <el-form-item label="序号" prop="id">
          <el-input v-model="opeForm.id" :disabled='true'></el-input>
        </el-form-item>
        <el-form-item label="留言人姓名" prop="name">
          <el-input v-model="opeForm.name"></el-input>
        </el-form-item>
        <el-form-item label="留言内容" prop="content">
          <el-input v-model="opeForm.content"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="opeDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="operation">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  created() {
    this.findMessageByDisplay()
  },
  data() {
    return {
      // 获取所有留言
      queryInfo: {
        display: -1,
        pageNum: 1,
        pageSize: 10,
        query: ""
      },
      // 根据 id 查询留言
      displayInfo: {
        pageNum: 1,
        pageSize: 10,
        display: 0
      },
      tableData: [],
      total: 0,
      opeDialogVisible: false,
      title: '',
      opeForm: {
        content: "",
        // 获取当前时间
        createtime: '',
        display: "",
        id: 0,
        name: ""
      },
      flag: 'unaudited',
      ids: []
    }
  },
  methods: {
    async getAllMessage() {
      this.flag = 'all'
      const { data: res } = await this.$http.post('findMessageByDisplay', this.queryInfo)
      if (res.code != 'success') return this.$message.error('获取留言失败')
      this.tableData = res.data
      this.total = res.numbers
    },
    addClick() {
      this.title = '添加留言'
      this.opeForm.content = ''
      this.opeForm.display = ''
      this.opeForm.name = ''
      this.opeForm.createtime = (new Date()).valueOf()
      this.opeDialogVisible = true
    },
    editClick(message) {
      this.title = '修改留言'
      this.opeForm.content = message.content
      this.opeForm.name = message.name
      this.opeForm.id = message.id
      this.opeForm.createtime = message.createtime
      this.opeDialogVisible = true
    },
    // 留言的增和改
    async operation() {
      if (this.title == '添加留言') {
        const { data: res } = await this.$http.post('insertMessage', this.opeForm)
        if (res !== 'success') return this.$message.error('添加留言失败')
        this.opeDialogVisible = false
        this.$message.success('添加留言成功')
        this.getAllMessage()
      } else {
        const { data: res } = await this.$http.post('updateMessageById', this.opeForm)
        if (res !== 'success') return this.$message.error('修改留言失败')
        this.$message.success('修改留言成功')
        this.opeDialogVisible = false
        this.getAllMessage()
      }
    },
    async delMessage(id) {
      const confirmResult = await this.$confirm('此操作将永久删除该留言, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmResult !== 'confirm') return this.$message.info('已取消删除')
      const { data: res } = await this.$http.post('batchDeleteMessage', { ids: id })
      if (res !== 'success') return this.$message.error('删除留言失败')
      this.$message.success('删除留言成功')
      this.getAllMessage()
    },
    // 根据姓名搜索留言
    async searchMessage() {
      const { data: res } = await this.$http.post('findAllMessage', this.queryInfo)
      if (res.code !== 'success') return this.$message.error('查询留言失败')
      this.$message.success('查询留言成功')
      this.tableData = res.data
    },
    // 发送请求前，初始化请求参数，避免上一次发送的请求参数影响到下一次
    init() {
      this.displayInfo.pageNum = 1
      this.displayInfo.pageSize = 10
      this.queryInfo.pageNum = 1
      this.queryInfo.pageSize = 10
      this.total = 0
      this.tableData = []
    },
    displayAll() {
      this.init()
      this.getAllMessage()
    },
    displayAudited() {
      this.init()
      this.flag = 'audited'
      this.findMessageByDisplay()
    },
    displayUnaudited() {
      this.init()
      this.flag = 'unaudited'
      this.findMessageByDisplay()
    },
    // 根据审核搜索留言
    async findMessageByDisplay() {
      if (this.flag == 'audited') {
        this.displayInfo.display = 1
      } else {
        this.displayInfo.display = 0
      }
      const { data: res } = await this.$http.post('findMessageByDisplay', this.displayInfo)
      if (res.code !== 'success') return this.$message.error('查询留言失败')
      this.$message.success('查询留言成功')
      this.tableData = res.data
      // console.log(this.tableData)
      this.total = res.numbers
    },

    // 单个反转
    async batchRever(id) {
      const { data: res } = await this.$http.post('reversalMessageDisplay', { ids: id })
      if (res !== 'success') return this.$message.error('反转留言状态失败')
      this.$message.success('反转留言状态成功')
      this.getAllMessage()
    },
    // 批量反转
    async batchRevers() {
      const { data: res } = await this.$http.post('reversalMessageDisplay', { ids: this.ids.join(',') })
      if (res !== 'success') return this.$message.error('批量反转留言状态失败')
      this.$message.success('批量反转留言状态成功')
      this.getAllMessage()
    },
    // 批量选择
    handleSelectionChange(val) {
      this.ids = []
      val.forEach(item => this.ids.push(item.id))
    },
    // 批量删除
    async batchDelete() {
      const confirmResult = await this.$confirm('此操作将永久删除这些留言，是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmResult !== 'confirm') return this.$message.info('已取消删除')
      const { data: res } = await this.$http.post('batchDeleteMessage', { ids: this.ids.join(',') })
      if (res !== 'success') return this.$message.error('删除留言失败')
      this.$message.success('删除留言成功')
      this.getAllMessage()
    },
    // 每页显示条数发生改变时
    handleSizeChange(size) {
      if (this.flag == 'all') {
        this.queryInfo.pageSize = size
        this.getAllMessage()
      } else {
        this.displayInfo.pageSize = size
        this.findMessageByDisplay()
      }
    },
    // 当前页码发生改变时
    handleCurrentChange(currentPage) {
      if (this.flag == 'all') {
        this.queryInfo.pageNum = currentPage
        this.getAllMessage()
      } else {
        this.displayInfo.pageNum = currentPage
        this.findMessageByDisplay()
      }
    },
  }
}
</script>
<style scoped>
.el-icon-arrow-down {
  margin-top: 12px;
}
</style>